/********************
* admission variables
* add variables to cohort tables for analysis (ones that don't exist up to this point); 
*
*******************/
%macro admissionvariables (cohortdata, allepisodes, outdata); 

/* discharge outcomes: */
/* died anytime during the episode */ 
/* 		died within 7 days */
/* discharged home */
/* discharged to long-term-care or rehab */
/* other */

/* death data */
data cohort;
	set &cohortdata;
	death_episode = 0;
	death_episode_7days = 0;
	if not missing(death_date) then do; 
		if death_date <= end_eoc then death_episode = 1; /* should be equal to, but just in case something weird happens have included prior */
		if (death_date - index_date) <= 7 and death_date <= end_eoc then death_episode_7days = 1;
	end; 
run;

/* discharge data */
* 1. get all episode data; 
proc sql; 
	create table cohort_fullepisode as
	select a.ppn, a.recnum, a.episode_of_care, a.episode_end_date, a.episode_end_time, a.start_eoc, a.index_flag, a.end_eoc, a.mode_of_separation_recode, a.episode_of_care_type
	from &allepisodes as a
	join &cohortdata as t
	on (a.ppn = t.ppn and a.episode_of_care = t.episode_of_care);  

* 2. sort within episodes; 	
proc sort data = cohort_fullepisode out = cohort_fullepisode; 
	by ppn episode_of_care episode_end_date episode_end_time; 
run; 

* 3. label discharge_home or discharge_longterm;
data cohort_fullepisode; 
	set cohort_fullepisode; 
	by ppn episode_of_care;
	discharge_home = 0; 
	discharge_nursinghome = 0;
	retain discharge_home discharge_nursinghome; 
	 
	if mode_of_separation_recode in ('0','1','10','2') then discharge_home = 1;
	/* transfer to nursing home, other accommodation or palliative unit  */ 
	else if mode_of_separation_recode in ('3','11','8') then discharge_nursinghome = 1; 
	
	if last.episode_of_care then output; 
run; 

proc sql; 
	create table cohort2 as
	select a.*, b.discharge_home, b.discharge_nursinghome
	from cohort as a
	left join cohort_fullepisode as b
	on (a.ppn = b.ppn and a.episode_of_care = b.episode_of_care); 

*4. label rehab (subset of longterm); 
proc sql;
	create table cohort_rehab as 
	select a.ppn, a.episode_of_care, a.mode_of_separation_recode, a.facility_trans_to_recode2, b.episode_of_care_type as transfer_rehab, b.facility_identifier_recode2
	from cohort as a
	left join &allepisodes as b
	on (a.ppn = b.ppn and a.facility_trans_to_recode2 = b.facility_identifier_recode2)
	/* transfer to rehab  */ 
	where a.mode_of_separation_recode in ('5','9') and b.episode_of_care_type = '2' and b.episode_of_care = a.episode_of_care + 1; 
quit; 
	
data cohort_rehab;
	set cohort_rehab; 
	by ppn episode_of_care; 
	rehab = 0; 
	retain rehab; 
	if transfer_rehab = '2' then rehab = 1; 
	if last.episode_of_care then output; 
run; 

*5. join back to cohort table; 
proc sql; 
	create table cohort as
	select a.*, b.rehab
	from cohort2 as a
	left join cohort_rehab as b
	on a.ppn = b.ppn and a.episode_of_care = b.episode_of_care;

data cohort; 
	set cohort; 
	discharge_rehab = 0; 
	if rehab = 1 then discharge_rehab = 1; 
run;
	
*6. change to discharge longterm; 
data cohort; 
	set cohort; 
	discharge_longterm = 0; 
	discharge_other = 0; 
	if discharge_rehab = 1 or discharge_nursinghome = 1 then discharge_longterm = 1; 
	
	if discharge_home = 0 and discharge_longterm = 0 and death_episode = 0 then discharge_other = 1; 
run; 

proc freq data = cohort; 
	tables discharge_home discharge_longterm death_episode discharge_other;
run; 

proc sql; 
	drop table cohort_fullepisode, cohort2, cohort_rehab; quit; 

/* readmission outcomes */
/* 30 day readmission & 90 day readmission (other than rehab care) */ 
*1. join episodes within 30 days and 90 days and not rehab.; 
data readmit_exclusion; 
	set &allepisodes;
	keep = 0; 
	ed_label = 0; 
	/* acute care and emergency admissions */ 
	if episode_of_care_type in ('1') then keep = 1; 
	if source_of_referral_recode = '01' then ed_label = 1; 
	if ed_status not in ('','3') then ed_label = 1;
	if ed_label = 0 then keep = 0; 
	if keep = 0 then delete; 
run; 

proc sql; 
	create table cohort_readmit as 
	select a.*, b.recnum as recnum_readmit_90, b.episode_start_date as readmit_start_date
	from cohort as a 
	left join (select ppn, recnum, episode_of_care, episode_of_care_type, episode_start_date, facility_trans_from_recode2
				from readmit_exclusion) as b
	on a.ppn = b.ppn and 
		a.episode_of_care ne b.episode_of_care and 
		((b.episode_start_date - a.end_eoc) between 0 and 90) and 
		(a.discharge_home = 1 and b.facility_trans_from_recode2 = ''); 

data cohort_readmit; 
	set cohort_readmit; 
	time_to_readmit = readmit_start_date - end_eoc; 
run; 

proc sort data = cohort_readmit; 
	by ppn episode_of_care; 
run; 

data cohort; 
	set cohort_readmit; 
	by ppn episode_of_care;
	readmit_30 = 0; 
	readmit_90 = 0; 
	retain readmit_30 readmit_90; 
	
	if recnum_readmit_90 ne '' and time_to_readmit <= 30 then readmit_30 = 1; 
	if recnum_readmit_90 ne '' then readmit_90 = 1; 
	
	if last.episode_of_care then output; 
run; 

proc sql; 
	drop table cohort_readmit; quit; 

*************************************; 
	* transfers within hospital ; 
data cohort; 
	set cohort; 
	transfer_within = 0; 
	if source_of_referral_recode in ('04','05') then transfer_within = 1; 
	/* transfer to public psych or other hospital */
	if mode_of_separation_recode in ('4','5') then transfer_within = 1; 
run; 

/* try and test something else */ 
proc sql; 
	create table cohort_transfers as
	select a.*, b.recnum as transfer_recnum
	from cohort as a
	left join &allepisodes as b
	on a.ppn = b.ppn and a.recnum ne b.recnum and a.episode_of_care = b.episode_of_care; 

data cohort; 
	set cohort_transfers; 
	by ppn episode_of_care; 
	transfer_within = 0; 
	retain transfer_within; 
	if transfer_recnum ne '' then transfer_within = 1; 
	if last.episode_of_care then output; 
run; 

/*******************/
/* hospital volume */ 
/*******************/
data cohort; 
	set cohort; 
	mon_year = put(index_date, MONYY7.);
	year = put(index_date, year4.); 
run; 

* months observed per year; 
proc sql; 
	create table month as
	select year, count(distinct(mon_year)) as observed_month
	from cohort
	group by year; 

data month; 
	set month; 
	rates = 12 / observed_month; 
run;

* hospitals; 
proc sql; 
	create table hospital as 
	select distinct facility_identifier_recode2
	from cohort; 

* full table (zero counts for means); 
proc sql; 
	create table hospital2 as 
	select h.facility_identifier_recode2, m.year, m.rates
	from hospital(keep = facility_identifier_recode2) h, month(keep=year rates) m
	order by h.facility_identifier_recode2,m.year; 

proc sql; drop table hospital; 

* number of procedures per hospital per year; 
proc sql;
	create table n_hospital as
	select facility_identifier_recode2, year, count(distinct(recnum)) as n
	from cohort
	group by facility_identifier_recode2,year; 

proc sql;
	create table hospital as 
	select h.*, n.n as count_procedures
	from hospital2 as h
	full outer join n_hospital as n
	on h.facility_identifier_recode2 = n.facility_identifier_recode2 and h.year = n.year; 

proc sql; drop table n_hospital; 

data hospital; 
	set hospital;
	if missing(count_procedures) then count_procedures = 0; 
run; 

* annualised number of procedures per hospital per year; 
data hospital; 
	set hospital; 
	annualised_n = rates*count_procedures; 
run; 

* mean number of procedures per hospital per year (annualised 12 months); 
proc sql; 
	create table hospital2 as
	select facility_identifier_recode2, mean(annualised_n) as av_proc_year
	from hospital 
	group by facility_identifier_recode2; 

* join to original table; 
proc sql; 
	create table temp1 as
	select t.*, h.av_proc_year
	from cohort as t
	join hospital2 as h
	on t.facility_identifier_recode2 = h.facility_identifier_recode2; 

data cohort; 
	set temp1; 
run; 

proc sql; drop table temp1, hospital2; 

data cohort; 
	length peer_group $7;
	format peer_group $7.;
	set cohort;
	if hospital_type in (2,4) and missing(peer_group) then peer_group = 'Private' ;
run; 

/**************************/
/* primary expected payer */ 
/**************************/
* use index procedure episode only; 
data cohort; 
	set cohort; 
	label primary_payer = "primary payer from payment status";
	length primary_payer $12;
	format primary_payer $12.;
	
	if payment_status_on_sep in ('20','23','24') then primary_payer = "public"; 
	*note 31 - 36 should be private patients in public hospitals; 
	*phi may be self-funded patients or those with phi; 
	else if payment_status_on_sep in ('30','31','32','33','34','35','36') then primary_payer = "phi"; 
	else if payment_status_on_sep in ('40','41','42') then primary_payer = "compensation"; 
	else if payment_status_on_sep in ('50','51') then primary_payer = "dva"; 
	else primary_payer = "other"; 

run; 

data &outdata; 
	set cohort; 
run; 

%mend admissionvariables; 
